package com.qq.spirit;

import java.util.Iterator;
import java.util.List;

import com.gargoylesoftware.htmlunit.util.NameValuePair;
import com.qq.spirit.inter.AbstractActionAfterHttpRequest;
import com.qq.spirit.inter.IActionAfterHttpRequest;
import com.qq.spirit.inter.TaskUnit;
import com.qq.spirit.model.UserData;
import com.qq.spirit.model.XmlDataUtil;
import com.qq.spirit.model.XmlNodeData;

public class SessionHolderTask implements Runnable
{

	@Override
	public void run()
	{
		try
		{
			Iterator<String> ite = UserCache.userQueue.keySet().iterator();
			while (ite.hasNext())
			{
				String username = ite.next();
				TaskManager.addTask(new SessionHolder(username));
			}
		} catch (Exception ex)
		{
			LoggerUtil.error(ex);
		}
	}
}

class SessionHolder extends TaskUnit
{

	public SessionHolder(String username)
	{
		super();
		this.username = username;
	}

	@Override
	public void doTask()
	{
		UserData u = UserCache.get(username);
		if (u.getUserState() == UserState.LOGIN_GAME)
		{
			HttpUtil.doGet(username, GameConstants.PLAY_USER_INFO_URL, new SessionHolderResult(username));
		} else
		{
			// 添加登录任务先
			TaskManager.addTask(new LoginTask(username, this));
		}
	}
}

/**
 * 会话保持
 * 
 * @author beedoorwei
 * 
 */
class SessionHolderResult extends AbstractActionAfterHttpRequest
{
	public SessionHolderResult(String username)
	{
		super();
		this.username = username;
	}

	@Override
	public void doCall(List<NameValuePair> headerList, XmlNodeData playInfo)
	{
		UserCache.get(username).setAllPlayInfo(playInfo);
	}
}
